package com.olda.play.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.olda.play.enums.ConstantEnum;
import com.olda.play.enums.ResponseCode;
import com.olda.play.model.PageResp;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysDictionaryItemBizService;
import com.olda.play.system.biz.dao.SysDictionaryItemBizMapper;
import com.olda.play.system.dto.SysDictionaryItemDto;
import com.olda.play.system.model.dao.SysDictionaryItemMapper;
import com.olda.play.system.model.po.SysDictionaryItem;
import com.olda.play.system.service.SysDictionaryItemService;
import com.olda.play.system.vo.SysDictionaryItemVo;
import com.olda.play.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 数据字典子项接口
 *  查询、新增、删除、编辑
 *
 * @author oldA
 *
 */
@Service
@RequestMapping(value =  "/dictItem")
@RestController
public class SysDictionaryItemServiceImpl implements SysDictionaryItemService {

    @Autowired
    private SysDictionaryItemBizMapper sysDictionaryItemBizMapper;
    @Autowired
    private SysDictionaryItemBizService sysDictionaryItemBizService;
    @Autowired
    private SysDictionaryItemMapper sysDictionaryItemMapper;

    /**
     * 数据字典子项列表分页查询
     *  一般数据字典编码不能为空
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysDictItemList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto querySysDictItemList(@RequestBody SysDictionaryItemVo param) {
        if(StringUtils.isBlank(param.getDictCode())){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Map<String,Object> map= BeanUtil.copyProperties(param, Map.class);
        map.put("isDeleted",ConstantEnum.IS_DELETE_NO.getCode());
        int pageNum = param.getPageNum();
        int pageSize = param.getPageSize();
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<SysDictionaryItemDto> list=sysDictionaryItemBizMapper.selectByCondition(map);
        PageInfo<SysDictionaryItemDto> pageInfo = new PageInfo(list);
        int count = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
        if (CollectionUtils.isEmpty(list)) {
//            return CommonResponseDto.success(PageResp.create(Collections.EMPTY_LIST, count, pageNum, pageSize));
            return CommonResponseDto.success(new PageInfo<>(new ArrayList<>()));
        }
//        return CommonResponseDto.success(PageResp.create(list, count, pageNum, pageSize));
        return CommonResponseDto.success(pageInfo);
    }

    /**
     * 根据id查询数据字典子项最新详情
     * @param id
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysDictItemById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<SysDictionaryItemDto> querySysDictItemById(@RequestParam Long id) {
        SysDictionaryItem sysDictionaryItem=sysDictionaryItemBizService.selectSysDictItemById(id);
        return CommonResponseDto.success(BeanUtil.copyProperties(sysDictionaryItem,SysDictionaryItem.class));
    }

    /**
     * 新增或修改数据字典子项
     * @param param
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @RequestMapping(value = "/addOrUpdateSysDictItem",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<Boolean> addOrUpdateSysDictItem(@RequestBody SysDictionaryItemVo param) {
        if(param==null){
            //新增-- 注意判断同一个业务字段编码下---字典子项编码是否重复

            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Long id=param.getId();
        if(id==null){
            SysDictionaryItem sysDictionaryItemOld=sysDictionaryItemBizService.selectSysDictItemByItemCode(param.getItemCode());
            if(sysDictionaryItemOld!=null){
                return CommonResponseDto.error(ResponseCode.MORE_DICT);
            }
            SysDictionaryItem sysDictionaryItem=BeanUtil.copyProperties(param,SysDictionaryItem.class);
            sysDictionaryItem.setCreateTime(new Date());
            sysDictionaryItem.setCreateUser(UserUtils.getUserId().toString());
            sysDictionaryItem.setCreateUserName(UserUtils.getUsername());
            sysDictionaryItem.setIsDeleted(ConstantEnum.IS_DELETE_NO.getCode());
            sysDictionaryItem.setPlatform(ConstantEnum.PLATEFORM_0.getCode());
            sysDictionaryItemMapper.insert(sysDictionaryItem);
        }else{
            SysDictionaryItem sysDictionaryItem=BeanUtil.copyProperties(param,SysDictionaryItem.class);
            sysDictionaryItemBizService.updateSysDictItemById(sysDictionaryItem);
        }
        return CommonResponseDto.success(true);
    }

    /**
     * 删除数据字典子项
     * 仅支持单条删除且需要二次确认弹窗
     * @param id
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @RequestMapping(value = "/deleteSysDictItemById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<Boolean> deleteSysDictItemById(@RequestParam Long id) {
        if(id==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        SysDictionaryItem sysDictionaryItem=sysDictionaryItemBizService.selectSysDictItemById(id);
        if(sysDictionaryItem==null){
            return CommonResponseDto.error(ResponseCode.HAS_NO_DATA);
        }
        sysDictionaryItemBizService.deleteSysDictItemById(id);
        return CommonResponseDto.success(true);
    }
}
